package mobileann.mafamily.utils;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.graphics.Bitmap;

public class ImageCache {

	HashMap<String, SoftReference<Bitmap>> cache ;

	public ImageCache() {
		cache =  new HashMap<String, SoftReference<Bitmap>>();
	}

	public Bitmap getBitmapFromCache(String path){
		if(cache == null) cache = new HashMap<String, SoftReference<Bitmap>>();
		SoftReference<Bitmap> softReference = cache.get(path);
		if (softReference == null) 	return null;
		Bitmap bitmap = softReference.get();
		if (bitmap == null) return null;
		return bitmap;
	}

	public boolean addBitmap(String path, Bitmap bitmap) {
		if(path == null) return false;
		if(bitmap == null) return false;
		if(cache == null) cache = new HashMap<String, SoftReference<Bitmap>>();
		SoftReference<Bitmap> softBmp = new SoftReference<Bitmap>(bitmap);
		cache.put(path, softBmp);
		return true;
	}

	public Bitmap delBitmap(String path) {
		if(path == null) return null;
		if(cache == null) return null;
		SoftReference<Bitmap> remove = cache.remove(path);
		if(remove == null) return null;
		return remove.get();
	}

	public boolean clear() {
		if(cache == null) return true;
		cache.clear();
		cache = null;
		return true;
	}
	
}
